package com.sy.service.Impl;

import com.sy.constant.Method;
import com.sy.dto.JsonResult;
import com.sy.exception.QuestionException;
import com.sy.entity.User;
import com.sy.repository.UserRepository;
import com.sy.service.UserService;
import com.sy.utils.HttpUtils;
import com.sy.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zbbstart
 * @date 2021/1/23 - 11:56
 */
@Service
@Transactional
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private HttpUtils httpUtils;

    @Override
    public User insert(User user) {
        if (StringUtils.isBlank(user.getUsername())) {
            throw new QuestionException("用户名不能为空！");
        }
        if (StringUtils.isBlank(user.getPassword())) {
            throw new QuestionException("密码不能为空！");
        }
        return userRepository.save(user);

    }

    @Override
    public void delete(Integer userId) {
        userRepository.deleteById(userId);
    }

    @Override
    public User findUserByUserId(Integer userId) {
        return userRepository.findById(userId).get();
    }

    @Override
    public User getUserInfoByUserName(String userName) {
        if (StringUtils.isBlank(userName)) {
            throw new QuestionException("用户名不能为空！");
        }
        return userRepository.getUserInfoByUserName(userName);
    }


    /**
     * 获取所有的用户信息列表
     *
     * @return 用户信息列表
     */
    @Override
    public List<User> getAllUsersInfo() {
        return userRepository.findAll();
    }
}
